package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.QueryStatisticsService;
import cc.rengu.igas.momp.facade.request.*;
import cc.rengu.igas.momp.facade.response.*;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 统计数据查询类接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/24 11:52
 */
@SuppressWarnings("unchecked")
public class QueryStatisticsServiceImpl implements QueryStatisticsService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryBankToClientMcStatResponse>
     * @Author liujinan
     * @Description 银行对客类营销活动统计报表查询Dubbo接口实现
     * @date 2020/4/1
     * @Param queryBankToClientMcStatRequest
     **/
    @Override
    public Result<QueryBankToClientMcStatResponse> queryBankToClientMcStat(QueryBankToClientMcStatRequest queryBankToClientMcStatRequest) {

        DubboService<QueryBankToClientMcStatRequest> dubboService = new DubboServiceImpl();
        Result<QueryBankToClientMcStatResponse> queryBankToClientMcStatResponseResultu = new Result<>();
        try {
            if (!MompTxnNumEnum.BANK_TO_CLIENT_MC_STAT_QUERY.getTxnNum().equals(queryBankToClientMcStatRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryBankToClientMcStatResponse> dubboResult = dubboService.callAtomService(queryBankToClientMcStatRequest,
                    MompTxnNumEnum.BANK_TO_CLIENT_MC_STAT_QUERY.getService(), QueryBankToClientMcStatResponse.class);
            queryBankToClientMcStatResponseResultu.setResult(dubboResult.getResult());
            queryBankToClientMcStatResponseResultu.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryBankToClientMcStatResponse queryBankToClientMcStatResponse = new QueryBankToClientMcStatResponse();
            queryBankToClientMcStatResponseResultu.setResult(queryBankToClientMcStatResponse);
            queryBankToClientMcStatResponse.setHeader(queryBankToClientMcStatRequest.getHeader());
            queryBankToClientMcStatResponseResultu.setSuccess(false);
            if (e instanceof BizException) {
                queryBankToClientMcStatResponse.setRespCode(((BizException) e).getCode());
                queryBankToClientMcStatResponse.setRespDesc(e.getMessage());
            } else {
                queryBankToClientMcStatResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryBankToClientMcStatResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryBankToClientMcStatResponseResultu;
    }

    /**
     * @param queryMchntToClientMcStatRequest
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryMchntToClientMcStatResponse>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/1
     * @Description: 商户对客类营销活动统计报表查询Dubbo实现
     */
    @Override
    public Result<QueryMchntToClientMcStatResponse> queryMchntToClientMcStat(QueryMchntToClientMcStatRequest queryMchntToClientMcStatRequest) {

        DubboService<QueryMchntToClientMcStatRequest> dubboService = new DubboServiceImpl();
        Result<QueryMchntToClientMcStatResponse> queryMchntToClientMcStatResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.MCHNT_TO_CLIENT_MC_STAT_QUERY.getTxnNum().equals(queryMchntToClientMcStatRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryMchntToClientMcStatResponse> dubboResult = dubboService.callAtomService(queryMchntToClientMcStatRequest,
                    MompTxnNumEnum.MCHNT_TO_CLIENT_MC_STAT_QUERY.getService(), QueryMchntToClientMcStatResponse.class);
            queryMchntToClientMcStatResponseResult.setResult(dubboResult.getResult());
            queryMchntToClientMcStatResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryMchntToClientMcStatResponse queryMchntToClientMcStatResponse = new QueryMchntToClientMcStatResponse();
            queryMchntToClientMcStatResponseResult.setResult(queryMchntToClientMcStatResponse);
            queryMchntToClientMcStatResponse.setHeader(queryMchntToClientMcStatRequest.getHeader());
            queryMchntToClientMcStatResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryMchntToClientMcStatResponse.setRespCode(((BizException) e).getCode());
                queryMchntToClientMcStatResponse.setRespDesc(e.getMessage());
            } else {
                queryMchntToClientMcStatResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryMchntToClientMcStatResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryMchntToClientMcStatResponseResult;
    }

    /**
     * @param queryMchnFeeDiscountDetailRequest
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryMchnFeeDiscountDetailResponse>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/1
     * @Description: 商户手续费优惠信息统计报表查询Dubbo实现
     */
    @Override
    public Result<QueryMchnFeeDiscountDetailResponse> queryMchnFeeDiscountDetail(QueryMchnFeeDiscountDetailRequest queryMchnFeeDiscountDetailRequest) {

        DubboService<QueryMchnFeeDiscountDetailRequest> dubboService = new DubboServiceImpl();
        Result<QueryMchnFeeDiscountDetailResponse> queryMchnFeeDiscountDetailResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.MCHNT_FEE_DISCOUNT_DETAIL.getTxnNum().equals(queryMchnFeeDiscountDetailRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryMchnFeeDiscountDetailResponse> dubboResult = dubboService.callAtomService(queryMchnFeeDiscountDetailRequest,
                    MompTxnNumEnum.MCHNT_FEE_DISCOUNT_DETAIL.getService(), QueryMchnFeeDiscountDetailResponse.class);
            queryMchnFeeDiscountDetailResponseResult.setResult(dubboResult.getResult());
            queryMchnFeeDiscountDetailResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryMchnFeeDiscountDetailResponse queryMchnFeeDiscountDetailResponse = new QueryMchnFeeDiscountDetailResponse();
            queryMchnFeeDiscountDetailResponseResult.setResult(queryMchnFeeDiscountDetailResponse);
            queryMchnFeeDiscountDetailResponse.setHeader(queryMchnFeeDiscountDetailRequest.getHeader());
            queryMchnFeeDiscountDetailResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryMchnFeeDiscountDetailResponse.setRespCode(((BizException) e).getCode());
                queryMchnFeeDiscountDetailResponse.setRespDesc(e.getMessage());
            } else {
                queryMchnFeeDiscountDetailResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryMchnFeeDiscountDetailResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryMchnFeeDiscountDetailResponseResult;
    }

    @Override
    public Result<QueryCashbackToCashierDetailResponse> queryCashbackToCashierDetail(QueryCashbackToCashierDetailRequest queryCashbackToCashierDetailRequest) {

        DubboService<QueryCashbackToCashierDetailRequest> dubboService = new DubboServiceImpl();
        Result<QueryCashbackToCashierDetailResponse> queryCashbackToCashierDetailResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_CASHIER_CASHBACK_DETAIL_BY_BANK.getTxnNum().equals(queryCashbackToCashierDetailRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryCashbackToCashierDetailResponse> dubboResult = dubboService.callAtomService(queryCashbackToCashierDetailRequest,
                    MompTxnNumEnum.QUERY_CASHIER_CASHBACK_DETAIL_BY_BANK.getService(), QueryCashbackToCashierDetailResponse.class);
            queryCashbackToCashierDetailResponseResult.setResult(dubboResult.getResult());
            queryCashbackToCashierDetailResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryCashbackToCashierDetailResponse queryCashbackToCashierDetailResponse = new QueryCashbackToCashierDetailResponse();
            queryCashbackToCashierDetailResponseResult.setResult(queryCashbackToCashierDetailResponse);
            queryCashbackToCashierDetailResponse.setHeader(queryCashbackToCashierDetailRequest.getHeader());
            queryCashbackToCashierDetailResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryCashbackToCashierDetailResponse.setRespCode(((BizException) e).getCode());
                queryCashbackToCashierDetailResponse.setRespDesc(e.getMessage());
            } else {
                queryCashbackToCashierDetailResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryCashbackToCashierDetailResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryCashbackToCashierDetailResponseResult;
    }

    /**
     * 查询当前周期商户可优惠额度 剩余可优惠额度 已使用优惠额度
     *
     * @param queryMchntFeeDiscountLimitRequest
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryMchntFeeDiscountLimitResponse>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/8/15 19:05
     */
    @Override
    public Result<QueryMchntFeeDiscountLimitResponse> queryMchntFeeDiscountLimit(QueryMchntFeeDiscountLimitRequest queryMchntFeeDiscountLimitRequest) {

        DubboService<QueryMchntFeeDiscountLimitRequest> dubboService = new DubboServiceImpl();
        Result<QueryMchntFeeDiscountLimitResponse> queryMchntFeeDiscountLimitResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_MCHNTFEEDISCOUNT_LIMIT.getTxnNum().equals(queryMchntFeeDiscountLimitRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryMchntFeeDiscountLimitResponse> dubboResult = dubboService.callAtomService(queryMchntFeeDiscountLimitRequest,
                    MompTxnNumEnum.QUERY_MCHNTFEEDISCOUNT_LIMIT.getService(), QueryMchntFeeDiscountLimitResponse.class);
            queryMchntFeeDiscountLimitResponseResult.setResult(dubboResult.getResult());
            queryMchntFeeDiscountLimitResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryMchntFeeDiscountLimitResponse queryMchntFeeDiscountLimitResponse = new QueryMchntFeeDiscountLimitResponse();
            queryMchntFeeDiscountLimitResponseResult.setResult(queryMchntFeeDiscountLimitResponse);
            queryMchntFeeDiscountLimitResponse.setHeader(queryMchntFeeDiscountLimitRequest.getHeader());
            queryMchntFeeDiscountLimitResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryMchntFeeDiscountLimitResponse.setRespCode(((BizException) e).getCode());
                queryMchntFeeDiscountLimitResponse.setRespDesc(e.getMessage());
            } else {
                queryMchntFeeDiscountLimitResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryMchntFeeDiscountLimitResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryMchntFeeDiscountLimitResponseResult;
    }
}
